﻿import cgi
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.api import users
from entities import *
from Entities import *

def Add(TenVaiTro):
    vaitro = VaiTro(TenVaiTro=TenVaiTro)
    vaitro.put()
    
def Get(VaiTroID=None):
    if VaiTroID != None:
        vaitros = VaiTro.get_by_id(VaiTroID)
    else:
        vaitros = VaiTro.all()
    # Lay theo key: 
    #vaitro = VaiTro.get(db.Key(VaiTroID))
    vaitro_list = []
    if hasattr(vaitros,'__iter__'):
        for vaitro in vaitros:
            vaitro_dict ={}
            vaitro_dict['VaiTroID'] = vaitro.key().id()
            vaitro_dict['TenVaiTro'] = vaitro.TenVaiTro
            vaitro_list.append(vaitro_dict)
    else:
        vaitro_dict = {}
        vaitro_dict ={}
        vaitro_dict['VaiTroID'] = vaitros.key().id()
        vaitro_dict['TenVaiTro'] = vaitros.TenVaiTro  
        vaitro_list.append(vaitro_dict)
    tuple_vaitro = tuple(vaitro_list)    
    return tuple_vaitro
    
def GetID(TenVaiTro):
    vaitros = VaiTro.gql('WHERE TenVaiTro = :1',TenVaiTro)
    if vaitros.count() != 0:
        for vaitro in vaitros:
            VaiTroID = vaitro.key().id()
    else:
        VaiTroID = None
    return VaiTroID
          